package app.fugema.shop.main.order.view;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.base.BaseTitleActivity;
import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.ActivityRecyclerviewListBinding;
import app.fugema.shop.databinding.ItemChangeCouponViewBinding;
import app.fugema.shop.main.online.vm.OnlineVM;
import app.fugema.shop.main.order.model.BaseCouponBean;
import app.fugema.shop.main.setting.model.EventMsg;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.lljjcoder.style.citylist.Toast.ToastUtils;


import java.util.List;

public class CouponSelectActivity extends BaseTitleActivity<ActivityRecyclerviewListBinding, OnlineVM>
{

    public static void start(Context context, List<BaseCouponBean> couponList)
    {
        Intent intent = new Intent(context, CouponSelectActivity.class);
        intent.putExtra("coupons", JacksonUtils.transBean2Json( couponList ));
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_list);
    }

    private SingleRecyclerAdapter<BaseCouponBean, ItemChangeCouponViewBinding> adapter;
    private List<BaseCouponBean> mCouponList;

    @Override
    protected void initView()
    {
        super.initView();

        setTitle("选择优惠券");

        adapter = new SingleRecyclerAdapter<BaseCouponBean, ItemChangeCouponViewBinding>() {
            @Override
            public int getLayoutId()
            {
                return R.layout.item_change_coupon_view;
            }

            @Override
            public void onBindViewHolder(ItemChangeCouponViewBinding binding, int position, BaseCouponBean bean)
            {
                super.onBindViewHolder(binding, position, bean);
                //binding.tvContent.setText("        " + Utils.getHtml2Text(bean.getSymptoms()));
                if( bean.getBaseId() == -1 )
                {
                    binding.tvNotUse.setVisibility(  View.VISIBLE  );
                    binding.clCouponView.setVisibility(View.GONE);
                }
                else
                {
                    binding.tvNotUse.setVisibility(  View.GONE  );
                    binding.clCouponView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, BaseCouponBean bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);
                //ToastUtils.showShortToast( CouponSelectActivity.this,bean.getBaseName() );
                EventMsg  couponMsg = new EventMsg();
                couponMsg.setCode(1010);
                couponMsg.setData(bean);
                LiveEventBus.get(AppConstants.EventKey.CONFIRM_ORDER_CLICK, EventMsg.class).post( couponMsg );
                finish();
            }
        };
        binding.rvRecyclerView.setAdapter(adapter);
        viewUtils.setStateLayout(binding.rvRecyclerView, this);
    }


    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        String jsonStr = intent.getStringExtra("coupons");
        if(UIUtils.isNotEmpty( jsonStr ))
        {
            mCouponList = JacksonUtils.getConverList( jsonStr , BaseCouponBean.class );
        }

        if( UIUtils.isNotEmpty( mCouponList ) )
        {
            BaseCouponBean notUse = new BaseCouponBean();
            notUse.setBaseId(-1);

            mCouponList.add(0,notUse);
            adapter.setDataList( mCouponList );

        }
    }
}
